
public class Palindrom {
	// Main our program
	// Read from command line
	public static void main(String[] args) {
		final int[][] SAMPLE = { 
				{ 100, 324}, 
				{ 128, 423}, 
				{ 11, 22 }, 
		};
		long start = System.nanoTime();
		for (int i = 0; i < 3; i++) {
			System.out.println(numberPalindrom(SAMPLE[i][0], SAMPLE[i][1], true));
		}
		System.out.println("TIME: " + (System.nanoTime() - start));

		start = System.nanoTime();
		for (int i = 0; i < 3; i++) {
			System.out.println(numberPalindrom(SAMPLE[i][0], SAMPLE[i][1], false));
		}
		System.out.println("TIME: " + (System.nanoTime() - start));
		
	}

	// Our method
	private static int numberPalindrom(final int start, final int end, boolean choice) {
		int number = 0;
		if (choice) {
			for (int i = start; i < end; i++) {
				// Well then, if match, increasing them :D
				if (checkPalindrom(i)) {
					number = number + 1;
				}
			}
		} else {
			for (int i = start; i < end; i++) {
				// Well then, if match, increasing them :D
				if (isPalindrome(i)) {
					number = number + 1;
				}
			}
		}
		// That is it
		return number;

	}

	private static boolean isPalindrome(int number) {
		String s = number + "";
		String reversed = (new StringBuffer(s)).reverse().toString();
		return s.equals(reversed);
	}

	/**
	 * Check a number is palindrom or not
	 * @param number
	 * @return
	 */
	private static boolean checkPalindrom(int number) {
		// < 10, return
		if (number < 10)
			return true;
		String numberString = Integer.toString(number);
		char[] numberChar = numberString.toCharArray();
		final int length = numberChar.length;
		for (int i = 0; i < (length / 2); i++) {
			// If got one case that does not match, false
			if (numberChar[i] != numberChar[length - 1 - i]) {
				return false;
			}
		}
		return true;
	}
	
}
